unit con_CEP;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao_Consulta, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids,
  DBCtrls, Db, DBTables, TFlatButtonUnit;

type
  TfrmCon_CEP = class(TFrmConsulta)
    rgLogr: TRadioGroup;
    rgTipo: TRadioGroup;
    qry: TQuery;
    dtsrc: TDataSource;
    dbLokpUF: TDBLookupComboBox;
    edEnd: TEdit;
    Label1: TLabel;
    DBGrid1: TDBGrid;
    tq: TQuery;
    ds: TDataSource;
    tqCEP_CODIGO: TStringField;
    GroupBox1: TGroupBox;
    DBText1: TDBText;
    Label2: TLabel;
    DBText2: TDBText;
    Label3: TLabel;
    tqLOGRADOURO: TStringField;
    tqUF: TStringField;
    tqCID_NOME: TStringField;
    tqENDERECO: TStringField;
    tqBAIRRO: TStringField;
    qry1: TQuery;
    dtsrc1: TDataSource;
    dbLokpCid: TDBLookupComboBox;
    Bevel1: TBevel;
    Bevel2: TBevel;
    DBText3: TDBText;
    Label4: TLabel;
    tqCOMPLEMENTO: TStringField;
    procedure rgLogrClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure rgTipoClick(Sender: TObject);
    procedure edEndKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormActivate(Sender: TObject);
    procedure BtnFecharClick(Sender: TObject);
    procedure dbLokpUFClick(Sender: TObject);
    procedure rgTipoExit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    xCEP: string;
  end;

var
  frmCon_CEP: TfrmCon_CEP;

implementation

uses clsCEP, Dm;

var
  CEP: TCEP;

{$R *.DFM}

procedure TfrmCon_CEP.rgLogrClick(Sender: TObject);
var
  Texto: string;
begin
  inherited;
  case rgTipo.ItemIndex of
    0: TexTo := '';
    1: Texto := dbLokpUF.Text;
    2: Texto := dbLokpCid.Text;
  end;
  tq.databasename := dmint.Db.DatabaseName;
  CEP.Consultar(rgTipo.ItemIndex, rgLogr.ItemIndex, edEnd.Text, texto, tq);
  tq.Open;
end;

procedure TfrmCon_CEP.FormCreate(Sender: TObject);
begin
  inherited;
  CEP := TCEP.Create;
end;

procedure TfrmCon_CEP.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  CEP.Free;
end;

procedure TfrmCon_CEP.rgTipoClick(Sender: TObject);
begin
  inherited;
  qry.DatabaseName := dmint.Db.DatabaseName;
  qry1.DatabaseName := dmint.Db.DatabaseName;
  dbLokpUF.TabStop := True;
  dbLokpCid.TabStop := True;

  if rgTipo.ItemIndex = 0 then begin
    dbLokpUF.Enabled := False;
    dbLokpCid.Enabled := False;
    DBGrid1.Columns[0].Visible := True;
    DBGrid1.Columns[1].Visible := True;
    dbLokpUF.TabStop := False;
    dbLokpCid.TabStop := False;
  end;
  if rgTipo.ItemIndex = 1 then begin
    qry.Active := False;
    qry.SQL.Clear;
    qry.SQL.Add('Select distinct UF from CEP order by UF');
    qry.Open;
    dbLokpUF.Enabled := True;
    dbLokpUF.Visible := True;
    dbLokpCid.Visible := False;
    dbLokpUF.ListSource := dtsrc;
    dbLokpUF.ListField := 'UF';
    dbLokpUF.KeyField := 'UF';
    DBGrid1.Columns[0].Visible := False;
    DBGrid1.Columns[1].Visible := True;
  end;
  if rgTipo.ItemIndex = 2 then begin
    qry1.Active := False;
    qry1.SQL.Clear;
    qry1.SQL.Add('Select distinct Cid_Nome from CEP order by Cid_Nome');
    qry1.Open;
    dbLokpCid.Enabled := True;
    dbLokpCid.Visible := True;
    dbLokpUF.Visible := False;
    dbLokpCid.ListSource := dtsrc1;
    dbLokpCid.ListField := 'Cid_Nome';
    dbLokpCid.KeyField := 'Cid_Nome';
    DBGrid1.Columns[1].Visible := False;
    DBGrid1.Columns[0].Visible := True;
  end;

end;

procedure TfrmCon_CEP.edEndKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
  tq.Locate('Endereco', edEnd.Text, [lopartialkey]);
end;

procedure TfrmCon_CEP.FormActivate(Sender: TObject);
var
  Texto: string;
begin
  inherited;
  { case rgTipo.ItemIndex of
    1: Texto:= dbLokpUF.Text;
    2: Texto:= dbLokpCid.Text;
   end;
   tq.databasename:=dmint.Db.DatabaseName;
   CEP.Consultar(rgTipo.ItemIndex,rgLogr.ItemIndex,edEnd.Text,texto,tq);
   tq.Open;  }
end;

procedure TfrmCon_CEP.BtnFecharClick(Sender: TObject);
begin
  inherited;
  xCEP := tq.FieldByName('CEP_Codigo').AsString;
end;

procedure TfrmCon_CEP.dbLokpUFClick(Sender: TObject);
var
  Texto: string;
begin
  inherited;
  case rgTipo.ItemIndex of
    0: Texto := '';
    1: Texto := dbLokpUF.Text;
    2: Texto := dbLokpCid.Text;
  end;
  tq.databasename := dmint.Db.DatabaseName;
  CEP.Consultar(rgTipo.ItemIndex, rgLogr.ItemIndex, edEnd.Text, texto, tq);
  tq.Open;
end;

procedure TfrmCon_CEP.rgTipoExit(Sender: TObject);
begin
  inherited;
  if rgTipo.ItemIndex = 0 then rgLogr.SetFocus;
end;

end.
